<?php
declare (strict_types=1);

namespace app\controller;

use app\model\RewardPunishment as RewardPunishmentModel;
use app\model\UserPoints as UserPointsModel;
use think\facade\Request;
use think\facade\Validate;

class RewardPunishment
{
    public function addRewardPunishment()
    {
        $data = Request::post();

        // 验证数据
        $validate = Validate::rule([
            'user_id' => 'require|integer',
            'type' => 'require|in:reward,punishment',
            'description' => 'max:1000',
            'points' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['error' => $validate->getError()], 400);
        }

        // 记录奖励或惩罚
        $rp = new RewardPunishmentModel();
        $rp->user_id = $data['user_id'];
        $rp->type = $data['type'];
        $rp->description = $data['description'];
        $rp->points = $data['points'];
        $rp->save();

        // 更新用户积分
        $userPoints = UserPointsModel::find($data['user_id']);
        if (!$userPoints) {
            $userPoints = new UserPointsModel();
            $userPoints->user_id = $data['user_id'];
        }

        if ($data['type'] == 'reward') {
            $userPoints->total_points += $data['points'];
        } else {
            $userPoints->total_points -= $data['points'];
        }

        $userPoints->save();

        return json(['message' => '奖惩记录成功', 'rp_id' => $rp->rp_id]);
    }

    // 更新奖励/惩罚
    public function update()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'id' => 'require|integer',
            'type' => 'require|in:reward,penalty',
            'description' => 'max:500',
            'points' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $rewardPunishment = RewardPunishmentModel::find($data['id']);
        if (!$rewardPunishment) {
            return json(['status' => 'error', 'message' => 'Reward/Punishment not found']);
        }

        $rewardPunishment->type = $data['type'];
        $rewardPunishment->description = $data['description'] ?? $rewardPunishment->description;
        $rewardPunishment->points = $data['points'];
        $rewardPunishment->save();

        return json(['status' => 'success', 'message' => 'Reward/Punishment updated successfully']);
    }

    // 删除奖励/惩罚
    public function delete()
    {
        $data = Request::post();

        $validate = Validate::rule([
            'id' => 'require|integer',
        ]);

        if (!$validate->check($data)) {
            return json(['status' => 'error', 'message' => $validate->getError()]);
        }

        $rewardPunishment = RewardPunishmentModel::find($data['id']);
        if (!$rewardPunishment) {
            return json(['status' => 'error', 'message' => 'Reward/Punishment not found']);
        }

        $rewardPunishment->delete();

        return json(['status' => 'success', 'message' => 'Reward/Punishment deleted successfully']);
    }
}

